package com.pay.trade.controller;

import com.pay.common.result.Result;
import com.pay.trade.dto.PayRequest;
import com.pay.trade.dto.PayResponse;
import com.pay.trade.service.TradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Slf4j
@RestController
@RequestMapping("/api/trade")
public class TradeController {
    
    @Autowired
    private TradeService tradeService;
    
    /**
     * 发起支付
     */
    @PostMapping("/pay")
    public Result<PayResponse> pay(@RequestBody PayRequest request) {
        log.info("发起支付请求：{}", request);
        return tradeService.pay(request);
    }
    
    /**
     * 查询交易
     */
    @GetMapping("/{tradeNo}")
    public Result<PayResponse> queryTrade(@PathVariable String tradeNo) {
        log.info("查询交易请求：{}", tradeNo);
        return tradeService.queryTrade(tradeNo);
    }
    
    /**
     * 发起退款
     */
    @PostMapping("/{tradeNo}/refund")
    public Result<PayResponse> refund(@PathVariable String tradeNo, @RequestParam String amount) {
        log.info("发起退款请求：{}，金额：{}", tradeNo, amount);
        return tradeService.refund(tradeNo, amount);
    }
    
    /**
     * 测试发起支付（简化版）
     */
    @PostMapping("/test/pay")
    public Result<PayResponse> testPay() {
        PayRequest request = new PayRequest();
        request.setOrderNo("ORD" + System.currentTimeMillis());
        request.setMerchantId("TEST_MERCHANT_001");
        request.setAmount(new BigDecimal("100.00"));
        request.setCurrency("CNY");
        request.setChannel("WECHAT");
        request.setDescription("测试支付");
        request.setNotifyUrl("http://localhost:8082/api/notify");
        request.setReturnUrl("http://localhost:8082/api/return");
        
        log.info("测试发起支付请求：{}", request);
        return tradeService.pay(request);
    }
}



